

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HTTPPage {
	private String page = null;

	/**
	 * Get a page.
	 * 
	 * @param url
	 *            The string representation of the URL. Must be escaped.
	 */
	public HTTPPage(String url) {
		makePage(url);
	}

	private void makePage(String url) {
		/* The full page returned. */
		StringBuilder thePage = new StringBuilder("");
		/* One line of the page. */
		String line = "";
		try {
			URL theURL = new URL(url);
			HttpURLConnection URLConn = (HttpURLConnection) theURL
					.openConnection();
			/* Fake the browser so we don't get named as a robot. */
			/*
			 * URLConn.setRequestProperty("User-agent",
			 * "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.5) Gecko/20070713 Firefox/2.0.0.5"
			 * );
			 */
			URLConn
					.setRequestProperty(
							"User-agent",
							"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.5) Gecko/20070713 Firefox/2.0.0.5");
			URLConn.setDoOutput(true);
			/* Receive the response from the server. */
			BufferedReader inReader = new BufferedReader(new InputStreamReader(
					URLConn.getInputStream()));
			/* Write out the page one line at a time. */
			while ((line = inReader.readLine()) != null) {
				thePage.append(line);
			}
			this.page = thePage.toString();
			/* Close the connection. */
			inReader.close();
		} catch (Exception e) {
			System.out.println("error: " + e.getMessage());
			e.printStackTrace();
			System.out.println("Trying again");
			makePage(url);
		}
	}

	/**
	 * Simple getter for the text of the page.
	 * 
	 * @return The full text of the HTML page.
	 */
	public String getPage() {
		return this.page;
	}
}
